﻿using UnityEngine;
using System.Collections;
using System;
using NCmd;

// 主角控制
public class MainRole : Role
{
    public override void Init(AddSceneCreature serverData, CreatureTableData tableData)
    {
        base.Init(serverData, tableData);

        //// 注册事件
        //FightUIMgr.Instance.BindingJoystickEvent(MoveByJoystick, MoveStop);
        //FightUIMgr.Instance.BindingTouchScene(onTouchScene);
        //FightUIMgr.Instance.BindingSkillBtn(onSkillBtn);
        //FightUIMgr.Instance.BindingSkillCDUpdate(GetLeftCD,GetMaxCD);

        // 注册自动选择目标回调
        _skillMgr.ReSelectTargetCallback = onSelect;
    }

    // 响应技能按钮
    private void onSkillBtn(int skillIndex)
    {
        CastSkill(skillIndex);
    }

    protected override void Update()
    {
        base.Update();
    }

    // 摇杆控制移动
    // 入参，移动方向
    public void MoveByJoystick(Vector2 dir)
    {
        // 根据方向算坐标点
        dir *= 2;
        var targetPos = transform.position + new Vector3(dir.x, 0, dir.y);

        PurposeTo(targetPos);
    }

    // 跳转场景
    public void JumpMap(int ID)
    {
        var cmd = new NCmd.JumpMap() { ID = ID };
        // 向服务器发送跳转请求
        Net.Instance.SendCmd(cmd);
    }

    // 点击场景事件
    private void onTouchScene(RaycastHit ret)
    {
        onSelect(null);

        // 点击地面
        if (ret.transform.gameObject.layer == LayerMask.NameToLayer("Ground"))
        {
            moveByTouchGround(ret.point);
            return;
        }

        // 访问npc
        Creature creature = ret.transform.GetComponent<Creature>();
        if (creature != null)
        {
            // 自己不能选中自己
            if (creature == this)
            {
                return;
            }

            onTouchCreature(creature);
        }
    }

    // 选中某个生命体，creature变量可能为null
    private void onSelect(Creature creature)
    {
        SelectMgr.Instance.Target = creature;
        // 去掉老目标的通知
        if (CurTarget != null)
        {
            //CurTarget.HpChangeCallback -= FightUIMgr.Instance.SetHeadTargetHp;
        }
        // 选中新目标
        CurTarget = creature;

        // 给新目标增加通知
        if (creature != null)
        {
            //creature.HpChangeCallback += FightUIMgr.Instance.SetHeadTargetHp;
        }
    }

    // 点击到npc
    private void onTouchCreature(Creature creature)
    {
        onSelect(creature);
        if (creature.CanBeAttack(this))
        {
            //能被我打，站着不动
            return;
        }

        var npc = creature as Npc;
        if (npc != null)
        {
            // 功能npc：直接跑过去访问
            PurposeTo(creature.transform.position, 1f, () => onVisitNpc(npc));
        }
        else
        {
            // 是玩家，跑过去但不访问
            PurposeTo(creature.transform.position, 1f);
        }
    }

    private void onVisitNpc(Npc npc)
    {
        // 访问npc
        Debug.Log("访问npc：" + npc.ServerData.Name);
        LookAt2D(npc.transform.position);
    }

    // 点地移动
    private void moveByTouchGround(Vector3 point)
    {
        PurposeTo(point);
    }
}
